class MyBitmap {

  //每一个word是一个long类型元素，对应64位二进制
  words;
  //bitmap的位数大小
  size;

  constructor(size) {
    this.size = size;
    this.words = new Array((this.getWordIndex(size - 1) + 1));
  }

  /**
   * 判断bitmap某一位的状态
   * @param bitIndex  位图的第bitIndex位
   */
  getBit(bitIndex) {
    if (bitIndex < 0 || bitIndex > this.size - 1) {
      throw new Error("超过bitmap有效范围");
    }
    const wordIndex = this.getWordIndex(bitIndex);
    return (this.words[wordIndex] & (1 << bitIndex)) !== 0;
  }

  /**
   * 把bitmap某一位设为真
   * @param bitIndex  位图的第bitIndex位
   */
  setBit(bitIndex) {
    if (bitIndex < 0 || bitIndex > this.size - 1) {
      throw new Error("超过bitmap有效范围");
    }
    const wordIndex = this.getWordIndex(bitIndex);
    this.words[wordIndex] |= (1 << bitIndex);
  }


  /**
   * 定位bitmap某一位所对应的word
   * @param bitIndex  位图的第bitIndex位
   */
  getWordIndex(bitIndex) {
    //右移6位，相当于除以64
    return bitIndex >> 6;
  }
}



function run() {
  const bitMap = new MyBitmap(128);
  bitMap.setBit(126);
  bitMap.setBit(75);
  console.log(bitMap.getBit(126));
  console.log(bitMap.getBit(78));
}

run();